package com.example.myapplication.ArrayAdapterInclude;

import android.app.AlertDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.myapplication.R;
import org.w3c.dom.Text;



public class ShopArrayAdapter extends ArrayAdapter<String> {
  public ShopArrayAdapter(Context context, String[] data) {
    super(context, R.layout.shopitems_view, data);
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
      convertView =
          LayoutInflater.from((getContext())).inflate(R.layout.singleitem_layout, parent, false);
    }
    TextView shopNameView=(TextView) convertView.findViewById(R.id.itemName);
    String name=getItem(position);
    shopNameView.setText(name);

    ImageView itemIcon = (ImageView) convertView.findViewById(R.id.itemIcon);

    int resourceId= convertView.getResources().getIdentifier(name,"mipmap",convertView.getContext().getPackageName());
    itemIcon.setImageResource(resourceId);

    Button button = (Button) convertView.findViewById(R.id.ItemDetail);
    button.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        builder.setTitle("商品详情:" + name);
        String displayStr="";
        switch (name) {
          case "boots":
            displayStr = "靴子很好穿";
            break;
          case "cloth":
            displayStr = "衣服很好穿呀";
            break;
          case "hat":
            displayStr = "帽子能遮阳,今天好大太阳呀";
            break;

        }
        builder.setMessage(displayStr);
        builder.setPositiveButton("确定", null);
        AlertDialog dialog=builder.create();
        dialog.show();

      }
    });

    //CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.checkBox);
    //checkBox.setOnClickListener(new View.OnClickListener() {
    //  @Override public void onClick(View v) {
    //    checkBox.toggle();
    //  }
    //});



    return convertView;
  }
}
